package com.sr.security.handler;

import com.sr.enums.ResponseCode;
import com.sr.security.filter.LoginUserFilter;
import com.sr.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xiaochi
 * @date 2022/5/2 17:57
 * @desc AccessAuthenticationEntryPoint 匿名用户(即未登录时访问资源为匿名访问)无权限处理器
 */
@Slf4j
public class AccessAuthenticationEntryPoint implements AuthenticationEntryPoint {

    private LoginUserFilter loginUserFilter;

    public AccessAuthenticationEntryPoint(LoginUserFilter loginUserFilter){
        this.loginUserFilter = loginUserFilter;
    }

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        this.loginUserFilter.responseJson(response, R.error(ResponseCode.TOKEN_ERROR.getCode(),ResponseCode.TOKEN_ERROR.getMessage()));
    }
}
